#include "pch.h"
#include "Material.h"
#include "ResourceManager.h"
#include "ScriptPlugin.h"
#include "RendererEngine.h"
namespace mini
{
	CMaterial::CMaterial()
	{
		mResourceType = Material;
	}

	CMaterial::~CMaterial()
	{
		for (auto item : mpShaders)
		{
			SafeRelease(item);
		}
		mpShaders.clear();
		for (auto item : mTextures)
		{
			SafeRelease(item);
		}
		for( auto item : mSampleState )
		{
			SafeRelease(item);
		}
	}



	void CMaterial::apply(DeviceContext* pDeviceContext)
	{
		auto item = mTextures.begin();
		for (size_t i = 0; i < mTextures.size(); ++i)
		{
			if (nullptr != (*item) )
			{
				(*item)->commitToGpu(pDeviceContext, i);
			}
			++item;
		}
	}

	bool CMaterial::initialiseSampler(D3D11_SAMPLER_DESC* pSampler)
	{
		// Load the texture in.
		CDevice* pDevice = CRendererEngine::getSinglePtr()->getDevice();
		
		// Create the texture sampler state.
		ID3D11SamplerState* pSampleState = nullptr;
		HRESULT result = pDevice->getDevice()->CreateSamplerState(pSampler, &pSampleState);
		if (FAILED(result))
		{
			return false;
		}
		mSampleState.push_back(pSampleState);
		return true;
	}

	ID3D11SamplerState* CMaterial::getSample(int index)const
	{
		if (index < mSampleState.size())
		{
			return mSampleState[index];
		}
		else
		{
			return nullptr;
		}
	}

}

